<?php
require_once( './DButil.php' );

$id = $_REQUEST['id'];
$name = $_REQUEST['userName'];
$arrs = $_REQUEST['power'];

// powerName 不能相同
$sql = "select * from t_role where powerName='{$name}'";
if ( query( $sql ) ) {
    $result = [
        'msg' => '角色名重复',
        'code' => 404,
        'success' => false
    ];
} else {
    if ( $id >= 10 ) {
        $sql1 = "insert into t_role (roleId,roleName) values ('10{$id}','{$name}')";
    } else {
        $sql1 = "insert into t_role (roleId,roleName) values ('100{$id}','{$name}')";
    }
    $add = update( $sql1 );

    foreach ( $arrs as $value ) {
        if ( $id >= 10 ) {
                $sql2 = "insert into t_role_power (roleId,powerId) values ('10{$id}','{$value}')";
            } else {
                $sql2 = "insert into t_role_power (roleId,powerId) values ('100{$id}','{$value}')";
            }
        $add1 = update( $sql2 );
    }
    if ( $add && $add1 ) {
        $result = [
            'msg' => '添加成功',
            'code' => 200,
            'success' => true
        ];
    } else {
        $result = [
            'msg' => '添加失败',
            'code' => 400,
            'success' => false
        ];
    }
}

header("Content-Type:application/json;charset=utf-8");
echo json_encode($arrs);

?>